package com.tencent.mm.opensdk.diffdev.a;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.util.Log;

import com.tencent.mm.opensdk.diffdev.OAuthErrCode;
import com.tencent.mm.opensdk.diffdev.OAuthListener;

import org.json.JSONObject;

@SuppressLint("LongLogTag")
final class f extends AsyncTask {
    static final class a {
        public String B;
        public int C;
        public OAuthErrCode t;

        a() {

        }


        public static a b(byte[] arg9) {
            String v1_1;
            a v0 = new a();
            if (arg9 != null && arg9.length != 0) {
                try {
                    v1_1 = new String(arg9, "utf-8");
                } catch (Exception v1) {
                    Log.e("MicroMsg.SDK.NoopingResult", String.format("parse fail, build String fail, ex = %s", v1.getMessage()));
                    v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                    return v0;
                }

                try {
                    JSONObject v2 = new JSONObject(v1_1);
                    v0.C = v2.getInt("wx_errcode");
                    Log.d("MicroMsg.SDK.NoopingResult", String.format("nooping uuidStatusCode = %d", Integer.valueOf(v0.C)));
                    switch (v0.C) {
                        case 402: {
                            v0.t = OAuthErrCode.WechatAuth_Err_Timeout;
                            return v0;
                        }
                        case 403: {
                            v0.t = OAuthErrCode.WechatAuth_Err_Cancel;
                            return v0;
                        }
                        case 404: {
                            v0.t = OAuthErrCode.WechatAuth_Err_OK;
                            return v0;
                        }
                        case 405: {
                            v0.t = OAuthErrCode.WechatAuth_Err_OK;
                            v0.B = v2.getString("wx_code");
                            return v0;
                        }
                        case 408: {
                            v0.t = OAuthErrCode.WechatAuth_Err_OK;
                            return v0;
                        }
                        case 500: {
                            v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                            return v0;
                        }
                        default:
                            v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                            return v0;
                    }
                } catch (Exception v1) {
                    Log.e("MicroMsg.SDK.NoopingResult", String.format("parse json fail, ex = %s", v1.getMessage()));
                    v0.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                }
            } else {
                Log.e("MicroMsg.SDK.NoopingResult", "parse fail, buf is null");
                v0.t = OAuthErrCode.WechatAuth_Err_NetworkErr;
            }

            return v0;
        }
    }

    private int A;
    private OAuthListener r;
    private String u;
    private String url;

    public f(String arg4, OAuthListener arg5) {

        this.u = arg4;
        this.r = arg5;
        this.url = String.format("https://long.open.weixin.qq.com/connect/l/qrconnect?f=json&uuid=%s", arg4);
    }

    protected final Object doInBackground(Object[] arg13) {
        a v0_2;
        int v11 = 2;
        if (this.u != null) {
            if (this.u.length() == 0) {
                Log.e("MicroMsg.SDK.NoopingTask", "run fail, uuid is null");
                v0_2 = new a();
                v0_2.t = OAuthErrCode.WechatAuth_Err_NormalErr;
                return v0_2;
            }

            do {
                label_16:
                if (this.isCancelled()) {
                    Log.i("MicroMsg.SDK.NoopingTask", "IDiffDevOAuth.stopAuth / detach invoked");
                    v0_2 = new a();
                    v0_2.t = OAuthErrCode.WechatAuth_Err_Auth_Stopped;
                    return v0_2;
                }

                StringBuilder v1 = new StringBuilder().append(this.url);
                String v0 = this.A == 0 ? "" : "&last=" + this.A;
                String v1_1 = v1.append(v0).toString();
                long v2 = System.currentTimeMillis();
                byte[] v0_1 = e.a(v1_1, 60000);
                long v4 = System.currentTimeMillis();
                v0_2 = a.b(v0_1);
                Object[] v8 = new Object[4];
                v8[0] = v1_1;
                v8[1] = v0_2.t.toString();
                v8[v11] = Integer.valueOf(v0_2.C);
                v8[3] = Long.valueOf(v4 - v2);
                Log.d("MicroMsg.SDK.NoopingTask", String.format("nooping, url = %s, errCode = %s, uuidStatusCode = %d, time consumed = %d(ms)", v8));
                if (v0_2.t != OAuthErrCode.WechatAuth_Err_OK) {
                    label_87:
                    Log.e("MicroMsg.SDK.NoopingTask", String.format("nooping fail, errCode = %s, uuidStatusCode = %d", v0_2.t.toString(), Integer.valueOf(v0_2.C)));
                    return v0_2;
                }

                this.A = v0_2.C;
                if (v0_2.C == g.UUID_SCANED.getCode()) {
                    this.r.onQrcodeScanned();
                    continue;
                }

                if (v0_2.C == g.UUID_KEEP_CONNECT.getCode()) {
                    continue;
                }
            }
            while (v0_2.C != g.UUID_CONFIRM.getCode());

            if (v0_2.B != null && v0_2.B.length() != 0) {
                return v0_2;
            }

            Log.e("MicroMsg.SDK.NoopingTask", "nooping fail, confirm with an empty code!!!");
            v0_2.t = OAuthErrCode.WechatAuth_Err_NormalErr;
            return v0_2;
        } else {
            label_8:
            Log.e("MicroMsg.SDK.NoopingTask", "run fail, uuid is null");
            v0_2 = new a();
            v0_2.t = OAuthErrCode.WechatAuth_Err_NormalErr;
        }

        return v0_2;
    }

    protected final void onPostExecute(Object arg4) {
        this.r.onAuthFinish(((a) arg4).t, ((a) arg4).B);
    }
}

